using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace One_Crappy_RPG
{
    public class TileLayer
    {
        public int[,] Cells;
        List<Texture2D> Textures;
        public float Alpha = 1.0f;
        public int Width, Height;

        public TileLayer(int[,] cells, List<Texture2D> textures)
        {
            Cells = cells;
            Textures = textures;
            Width = cells.GetLength(1);
            Height = cells.GetLength(0);
        }

        public TileLayer(int height, int width)
        {
            Cells = new int[height, width];
            Height = height;
            Width = width;
        }

        public void LoadTextures(ContentManager content, params string[] names)
        {
            Texture2D texture;
            Textures = new List<Texture2D>();

            foreach (string n in names)
            {
                texture = content.Load<Texture2D>("Art/Tiles/" + n);
                Textures.Add(texture);
            }
        }

        public void SetCellIndex(int cellX, int cellY, int index)
        {
            Cells[cellY, cellX] = index;
        }

        public int GetCellIndex(int cellX, int cellY)
        {
            return Cells[cellY, cellX];
        }

        public int GetCellIndex(Creature target)
        {
            int targetCellX = (int)target.Position.X / TileMap.TileSize;
            int targetCellY = (int)target.Position.Y / TileMap.TileSize;

            return Cells[targetCellY, targetCellX];
        }

        public void Draw(SpriteBatch batch)
        {
            for (int y = 0; y < Cells.GetLength(0); y++)
            {
                for (int x = 0; x < Cells.GetLength(1); x++)
                {
                    int textureIndex = GetCellIndex(x, y);

                    if (textureIndex > -1)
                    {
                        Texture2D drawMe = Textures[textureIndex];

                        batch.Draw(drawMe,
                                   new Rectangle(x * TileMap.TileSize,
                                                 y * TileMap.TileSize,
                                                 TileMap.TileSize,
                                                 TileMap.TileSize),
                                   Color.White);
                    }
                }
            }
        }
    }
}
